from base_data_mock.bank_config import card_len_list, credits_card_len_list
from numpy import random


def random_bank_card(card_type="bank", card_bin="623668"):
    """
    随机生成银行卡号
    默认工商银行
    """
    global card_len
    if card_type == 'bank':
        card_len = card_len_list[card_bin]

    elif card_type == 'credits':
        card_len = credits_card_len_list[card_bin]

    import time
    ran_timestamp = str(round(time.time()))
    if len(card_bin) != 6:
        card_len = card_len - len(card_bin) + 6
    bank_card = str(card_bin) + ran_timestamp[0:(card_len - 9)] + str(random.randint(10, 99))
    sumOdd = sumEven = 0
    count = 0

    for bank_card_value in bank_card[::-1]:
        if (count % 2) == 0:
            if int(bank_card_value) * 2 > 9:
                sumEven += (int(bank_card_value) * 2 - 9)
            else:
                sumEven += int(bank_card_value) * 2
        else:
            sumOdd += int(bank_card_value)
        count = count + 1
    if (sumEven + sumOdd) % 10 != 0:
        last_digit = 10 - (sumEven + sumOdd) % 10
    else:
        last_digit = 0
    bank_card = bank_card + str(last_digit)
    return bank_card
